package controller

import (
	"github.com/gin-gonic/gin"
	"main/app/model"
	"net/http"
	//"fmt"
)

type searchData struct {
	Title string `json:"title"`
}

type articleData struct {
	Id string `json:"id"`
}

func FindArticle(c *gin.Context){
	var articleJSON articleData
	if err := c.ShouldBindJSON(&articleJSON);err != nil{
		c.JSON(http.StatusOK,gin.H{
			"message":err.Error(),
			"data":"",
		})
	}
	article,err := model.FindArticle(articleJSON.Id)
	if err!=nil{
		c.JSON(http.StatusOK,gin.H{
			"message":err.Error(),
			"data" :"",
		})
	}else{
		c.JSON(http.StatusOK,gin.H{
			"message":"",
			"data":article,
		})
	}
}

func SelectArticle(c *gin.Context){
	var search searchData
	if err := c.ShouldBindJSON(&search);err!=nil{
		c.JSON(http.StatusOK,gin.H{
			"message":err,
			"data":"",
		})
		return
	}
	article,err := model.SelectArticle(search.Title)
	if err!=nil{
		c.JSON(http.StatusOK,gin.H{
			"message":err,
			"data":"",
		})
	}else{
		c.JSON(http.StatusOK,gin.H{
			"message":"",
			"data":article,
		})
	}
}